from django.shortcuts import render,redirect
from utils.tencent.cos import delete_bucket
from web import models

def setting(request,project_id):
    """ 设置项目 """
    return render(request,'manage/setting.html')

def delete(request,project_id):
    """ 删除项目 """
    if request.method == 'GET':
        return render(request, 'manage/setting_delete.html')
    project_name = request.POST.get('project_name')
    if not project_name or project_name != request.tracer.project.name:
        return render(request, 'manage/setting_delete.html',{'error':"项目名称输入错误"})
    # 如果项目名正确则可以删除项目，并且只有项目的创建者才能删除
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'manage/setting_delete.html', {'error': "只有项目创建者才能删除项目"})

    # 以上验证都通过了则删除项目，并且删除跟项目一起创建的桶
    # 删除桶。先要删除桶中的所有文件和所有的文件碎片，才能删除桶
    delete_bucket(request.tracer.project.bucket,request.tracer.project.region)
    models.Project.objects.filter(id=project_id).delete()
    return redirect('project_list')

